import { _decorator, Animation, Button, Component, instantiate, Label, Node, Prefab } from 'cc';
import EventExt from 'db://cocos-ext/base/EventExt';
import EventHelper from 'db://cocos-ext/base/EventHelper';
import { UtilCocos } from 'db://cocos-ext/base/UtilCocos';
import { games, players } from 'yycore';

import SlotBetModule from './SlotBetModule';
import SlotBtnChangeBet from './SlotBtnChangeBet';
import { SLOT_SPIN_BTN_ENABLE } from './SlotEventType';

const { ccclass, property } = _decorator;
@ccclass('SlotBetControl')
export default class SlotBetControl extends Component {

    @property(Prefab)
    private readonly btnChangePfb: Prefab = null;

    @property(Button)
    private readonly btnMax: Button = null;

    @property(Label)
    private betLabel: Label = null;

    @property(Animation)
    private maxEffect: Animation = null;

    @property(Animation)
    private maxUnLockAnim: Animation = null;


    // 加减号按钮
    private btnSub: SlotBtnChangeBet = null;
    private btnPlus: SlotBtnChangeBet = null;

    private initChangeBetBtn(isPlus: boolean, clickFunc: () => void): SlotBtnChangeBet {
        const node = instantiate(this.btnChangePfb);
        this.node.addChild(node);
        const comp = node.getComponent(SlotBtnChangeBet);
        comp.isPlus = isPlus;
        comp.btnEvent = clickFunc;
        return comp;
    }


    onLoad() {
        this.btnPlus = this.initChangeBetBtn(true, this.onAddBtnClick.bind(this));
        this.btnSub = this.initChangeBetBtn(false, this.onMinusBtnClick.bind(this))
        this.btnMax.node.on(Button.EventType.CLICK, this.onMaxBtnClick, this);
        this.maxEffect.node.active = false;
        this.hideMaxUnlockAnim();
        // 修改押注数值按钮是否启用
        EventExt.on(SLOT_SPIN_BTN_ENABLE, (bol: boolean) => {
            this.btnSub.btnStatus = bol;
            this.btnPlus.btnStatus = bol;
            UtilCocos.setNodeGray(this.btnMax.node, !bol);
        }, this);
    }

    private _oldUnlockMax: number = 0;
    private isPlayMaxUnlockAnim: boolean = false;

    public start() {
        this.betLabel.string = games.gf.formatCoin(SlotBetModule.betValue);
        let self = this;
        this._oldUnlockMax = SlotBetModule.maxUnlockBetLv;
        this.scheduleOnce(() => {
            self.slotValueChange(false);
        }, 0.1)
        players.myself.on(this, players.events.ExpChanged, this.onExpChanged);
    }

    private onAddBtnClick() {
        SlotBetModule.curUiBetLv++;
        this.slotValueChange();
        if (SlotBetModule.curUiBetLv == this._oldUnlockMax)
            this.playMaxEffect();
        this.hideMaxUnlockAnim();
    }

    private onMinusBtnClick() {
        SlotBetModule.curUiBetLv--;
        this.slotValueChange();
    }

    private onMaxBtnClick() {
        if (SlotBetModule.curUiBetLv != this._oldUnlockMax)
            this.playMaxEffect();
        SlotBetModule.curUiBetLv = SlotBetModule.maxUnlockBetLv;
        this.slotValueChange();
        this.hideMaxUnlockAnim();
    }

    private slotValueChange(playSound: boolean = true) {
        this.btnPlus.refresh();
        this.btnSub.refresh();
        this.betLabel.string = games.gf.formatCoin(SlotBetModule.betValue)
        if (playSound) {
            EventHelper.playAudio("unLock" + (SlotBetModule.curUiBetLv + 1));
        }

    }

    private onExpChanged() {
        let bol = this.btnPlus.onExpChanged();
        this.btnSub.onExpChanged();
        let maxIndex = SlotBetModule.maxUnlockBetLv;
        if (maxIndex > this._oldUnlockMax) {
            this._oldUnlockMax = maxIndex;
            EventHelper.playAudio("unLock" + (maxIndex + 1));
            if (!this.isPlayMaxUnlockAnim) {
                this.isPlayMaxUnlockAnim = true;
                if (bol) this.scheduleOnce(this.playMaxUnlockAnim, 2.5)
                else this.playMaxUnlockAnim();
            }

        }
    }

    private playMaxUnlockAnim() {
        if (this.maxUnLockAnim.node.active) return;
        this.maxUnLockAnim.node.active = true;
        this.maxUnLockAnim.play();
        this.scheduleOnce(this.hideMaxUnlockAnim, 7);
    }

    private hideMaxUnlockAnim() {
        this.unschedule(this.hideMaxUnlockAnim);
        this.unschedule(this.playMaxUnlockAnim);
        this.maxUnLockAnim.node.active = false;
        this.maxUnLockAnim.stop();
        this.isPlayMaxUnlockAnim = false;
    }

    private playMaxEffect() {
        this.maxEffect.node.active = true;
        this.maxEffect.on(Animation.EventType.FINISHED, this.onMaxAnimFinished, this);
        this.maxEffect.play();
    }

    private onMaxAnimFinished() {
        this.maxEffect.node.active = false;
    }
}

